package group.igfd.manager.biz.service.impl;

import group.igfd.manager.biz.entity.OrderItemEntity;
import group.igfd.manager.biz.model.OrderItemModel;
import group.igfd.manager.biz.model.OrderModel;
import group.igfd.manager.biz.model.QueryOrderModel;
import group.igfd.manager.biz.service.OrderItemService;
import group.igfd.manager.biz.service.ISysConfigService;
import group.igfd.manager.biz.utils.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import group.igfd.manager.biz.dao.OrdersDao;
import group.igfd.manager.biz.entity.OrdersEntity;
import group.igfd.manager.biz.service.OrdersService;


@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersDao, OrdersEntity> implements OrdersService {

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private ISysConfigService ISysConfigService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<OrdersEntity> page = this.selectPage(
                new Query<OrdersEntity>(params).getPage(),
                new EntityWrapper<OrdersEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils findList(QueryOrderModel params) {
        Integer size = 10;
        Integer currentPage = 1;

        if (params.getLimit() != null) {
            size = params.getLimit();
        } else {
            params.setLimit(size);
        }

        if (params.getPage() != null) {
            currentPage = params.getPage();
        } else {
            params.setPage(currentPage);
        }

        if (params.getOrderNo() != null) {
            params.setOrderNo("%" + params.getOrderNo() + "%");
        }
        List<OrderModel> orderList = baseMapper.getOrderList(params);

        EntityWrapper<OrdersEntity> ordersEntityEntityWrapper = new EntityWrapper<>();
        if (params.getOrderNo() != null) {
            ordersEntityEntityWrapper.like("order_no",params.getOrderNo());
        }
        if (params.getStartTime() != null) {
            ordersEntityEntityWrapper.gt("created_at",params.getStartTime());
        }
        if (params.getEndTime() != null) {
            ordersEntityEntityWrapper.lt("created_at",params.getEndTime());
        }
        if (params.getStatus() != null) {
            ordersEntityEntityWrapper.eq("status",params.getStatus());
        }

        for (OrderModel orderModel : orderList) {
            //获取原商品总价
            EntityWrapper<OrderItemEntity> orderItemEntityEntityWrapper = new EntityWrapper<>();
            orderItemEntityEntityWrapper.eq("order_id",orderModel.getId());
            List<OrderItemEntity> orderItemEntities = orderItemService.selectList(orderItemEntityEntityWrapper);

            double goodsTotalEur = 0;
            double goodsTotalCny = 0;
            double eurRate = 0;

            double goodsDiscountPrice = 0;
            double couponDiscountPrice = 0;
            double birthdayMonthDiscountPrice = 0;
            double freight = 0;

            if ("0".equals(orderModel.getState()+"") || "1".equals(orderModel.getState()+"")
                    || "11".equals(orderModel.getState()+"") || "-1".equals(orderModel.getState()+"") || "-2".equals(orderModel.getState()+"")) {
                //未付款
                eurRate = ISysConfigService.getEurToCnyRate();
            } else {
                //已经付款
                eurRate = Double.valueOf(orderModel.getEurRate());
                couponDiscountPrice = orderModel.getCouponDiscountPrice();
            }

            for (OrderItemEntity orderItemEntity : orderItemEntities) {

                goodsDiscountPrice = goodsDiscountPrice + Math.round(orderItemEntity.getPriceEur() * (1 - orderItemEntity.getDiscount()) * orderItemEntity.getNumber() * eurRate);
                goodsTotalCny = goodsTotalCny + Math.round(orderItemEntity.getPriceEur() * orderItemEntity.getNumber() * eurRate);
                goodsTotalEur = orderItemEntity.getPriceEur() * orderItemEntity.getNumber() + goodsTotalEur;
            }
            orderModel.setGoodsTotalEur(goodsTotalEur);
            orderModel.setGoodsTotalCny(goodsTotalCny);
            orderModel.setEurRate(new DecimalFormat("#0.00").format(eurRate));
            orderModel.setGoodsDiscountPrice(goodsDiscountPrice);
            orderModel.setCouponDiscountPrice(couponDiscountPrice);
            orderModel.setBirthdayMonthDiscountPrice(birthdayMonthDiscountPrice);
            orderModel.setDiscountTotalPrice(goodsDiscountPrice + couponDiscountPrice + birthdayMonthDiscountPrice);

            if ("0".equals(orderModel.getState()+"") || "1".equals(orderModel.getState()+"")
                    || "11".equals(orderModel.getState()+"") || "-1".equals(orderModel.getState()+"") || "-2".equals(orderModel.getState()+"")) {
                //未付款
                freight = CommonConstant.countFreightCost((int)(goodsTotalCny - goodsDiscountPrice));
            } else {
                //已经付款
                freight = orderModel.getFreight();
            }

            orderModel.setFreight((int) freight);
            orderModel.setAmountPaid(goodsTotalCny - goodsDiscountPrice - couponDiscountPrice - birthdayMonthDiscountPrice + freight);
        }

        PageUtils pageUtils = new PageUtils(orderList,this.selectCount(ordersEntityEntityWrapper),size,currentPage);

        return pageUtils;
    }

    @Override
    public List<OrderItemModel> findOrderItems(String orderNo) {
        return baseMapper.getOrderItemList(orderNo);
    }


}
